package com.repair.worker.controller;

import com.repair.api.worker.response.data.WorkerInfo;
import com.repair.base.result.JsonResult;
import com.repair.satoken.utils.StpKit;
import com.repair.worker.pojo.dto.WorkerUpdateParam;
import com.repair.worker.service.OSSService;
import com.repair.worker.service.WorkerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张强
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/worker/")
@Api(tags = "维修员")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private OSSService ossService;

    @ApiOperation(value = "获取维修员信息")
    @GetMapping("getWorkerInfo")
    public JsonResult<WorkerInfo> getWorkerInfo(){
        Long workId = Long.valueOf((String) StpKit.WORKER.getLoginId());
        WorkerInfo worker = workerService.selectWorkerInfo(workId);
        return JsonResult.ok(worker);
    }

    @ApiOperation(value = "修改维修员昵称")
    @PostMapping("/updateWorkerNickName")
    public JsonResult<String> updateWorkerNickName(@RequestBody WorkerUpdateParam workerUpdateParam) {
        workerService.updateWorkerNickName(workerUpdateParam);
        return JsonResult.ok("修改成功");
    }

    @ApiOperation(value = "修改维修员头像")
    @PostMapping("uploadWorkerProfile")
    public JsonResult<String> uploadWorkerProfile(@RequestBody WorkerUpdateParam workerUpdateParam) throws IOException {
        workerService.uploadWorkerProfile(workerUpdateParam);
        return JsonResult.ok("修改成功");
    }

    @ApiOperation(value = "上传头像")
    @PostMapping("/upload")
    public ResponseEntity<ResponseData> upload(@RequestParam(value = "file") MultipartFile file) throws IOException {
            String url = ossService.uploadFile(file);
            System.out.println(url);
            return ResponseEntity.ok(new ResponseData(url));

    }

    // ResponseData 类
    @Data
    public class ResponseData {
        private String fileUrl;

        public ResponseData(String fileUrl) {
            this.fileUrl = fileUrl;
        }
    }


}
